package com.eastfair.common.service.impl;

import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eastfair.common.dao.AttachmentFileMapper;
import com.eastfair.common.dto.AttachmentFileDTO;
import com.eastfair.common.entity.AttachmentFile;
import com.eastfair.common.service.AttachmentFileService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.core.context.ContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 系统附件表
 * </p>
 *
 * @author clm
 * @date 2022-06-09
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class AttachmentFileServiceImpl extends SuperServiceImpl<AttachmentFileMapper, AttachmentFile> implements AttachmentFileService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<AttachmentFile> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(AttachmentFile model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTeamId(ContextUtil.getTeamId());
        return R.successDef();
    }


    /**
     * 查询附件
     * @param attachmentFileDTO
     * @return
     */
    @Override
    public List<AttachmentFile> query(AttachmentFileDTO attachmentFileDTO) {
        LambdaQueryWrapper<AttachmentFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(attachmentFileDTO.getBusinessId() != null, AttachmentFile::getBusinessId, attachmentFileDTO.getBusinessId());
        lambdaQueryWrapper.eq(!StrUtil.isBlank(attachmentFileDTO.getBusinessType()), AttachmentFile::getBusinessType, attachmentFileDTO.getBusinessType() );
        lambdaQueryWrapper.like(!StrUtil.isBlank(attachmentFileDTO.getAttachmentName()), AttachmentFile::getAttachmentName, attachmentFileDTO.getAttachmentName());
        lambdaQueryWrapper.between(attachmentFileDTO.getStartTime() != null && attachmentFileDTO.getEndTime() != null, AttachmentFile::getCreateTime, attachmentFileDTO.getStartTime(), attachmentFileDTO.getEndTime());
        lambdaQueryWrapper.eq(AttachmentFile::getIsDeleted, BusinessConstant.DELETE_NO);
        lambdaQueryWrapper.in(attachmentFileDTO.getBusinessIdList() != null && !attachmentFileDTO.getBusinessIdList().isEmpty(), AttachmentFile::getBusinessId, attachmentFileDTO.getBusinessIdList());
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 批量查询
     * @param ids
     * @return
     */
    @Override
    public List<AttachmentFile> queryBatch(List<Long> ids) {
        LambdaQueryWrapper<AttachmentFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in( AttachmentFile::getId, ids);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public AttachmentFile findById(Long id) {
        return baseMapper.selectById(id);
    }
}
